﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using DBAccess;
using System.Collections.Generic;

namespace SD.Database
{
    public  class CashFunctions
    {
        public static void SpendAll(int pId)
        {
            string command = "";
            if (UserFunctions.PlayerHasSpecialBulding(pId, Database.SpecialBuldings.Market) == true)
                command = "Update players set Cash = Cash + (select sum(ProductionPoints) from planets where OwnerId = @param0) * @param1 where PlayerId = @param0";
            else
                command = "Update players set Cash = Cash + (select sum(ProductionPoints) from planets where OwnerId = @param0) where PlayerId = @param0";
            DAL.GetInstance().UpdateData(command, new List<object>() { pId , 1.05});
            DAL.GetInstance().UpdateData("Update planets set ProductionPoints = 0 where OwnerId = @param0", new List<object>() { pId });
        }

        public static void AddCash(int pId, int ammount)
        {
            DAL.GetInstance().UpdateData("Update players Set Cash = Cash + @param0 Where PlayerId = @param1", new List<object>() { ammount, pId });
        }
 
        public static void SendMoneyToCapital(int pId)
        {
            int sId, plId;
            UserFunctions.PlayerCapital(pId, out sId, out plId);
            double cash = UserFunctions.GetPlayerFloat(pId, "Cash") * 0.952;
            if (sId == -1 || plId == -1) return;

            DAL.GetInstance().UpdateData("Update players set Cash = 0 Where PlayerId = @param0", new List<object>() { pId });
            DAL.GetInstance().UpdateData("Update planets set ProductionPoints =ProductionPoints + @param0 where PlanetId = @param1 and SystemId = @param2", new List<object>() { cash, plId, sId });
        }
    };
}